package com.foxmail.aroundme.boring.weight;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import com.foxmail.aroundme.boring.BuildConfig;
import com.socks.library.KLog;

/**
 * 添加了滑动到底部自动加载的RecyclerView
 */
public class AutoLoadMoreRecyclerView extends RecyclerView {

    //正在加载更多状态
    public static final int STATE_MORE_LOADING = 1;
    //准备加载更多状态
    public static final int STATE_MORE_LOADED = 2;

    private int mCurrentState = STATE_MORE_LOADED;

    /**
     * 是否正在加载底部
     *
     * @return
     */
    public boolean isMoreLoading() {
        return mCurrentState == STATE_MORE_LOADING;
    }

    public AutoLoadMoreRecyclerView(Context context) {
        super(context);
    }

    public AutoLoadMoreRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoLoadMoreRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    private void init() {
        super.addOnScrollListener(new OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);

                if (mLoadMoreListener == null) {
                    return;
                }

                if (mCurrentState != STATE_MORE_LOADED || RecyclerView.SCROLL_STATE_IDLE != newState) {
                    return;
                }

                if (calculateRecyclerViewFirstPosition() == getAdapter().getItemCount() - 1) {
                    // 之前的状态为非正在加载状态
                    if (BuildConfig.DEBUG) {
                        KLog.i("加载更多数据");
                    }
                    mCurrentState = STATE_MORE_LOADING;
                    mLoadMoreListener.loadMore();
                }

            }
        });
    }

    /**
     * 计算RecyclerView当前第一个完全可视位置
     */
    private int calculateRecyclerViewFirstPosition() {
        return ((LinearLayoutManager) getLayoutManager()).findLastCompletelyVisibleItemPosition();
    }

    /**
     * 通知底部加载完成了
     */
    public void notifyMoreLoaded() {
        mCurrentState = STATE_MORE_LOADED;
    }

    public void setOnLoadMoreListener(OnLoadMoreListener loadMoreListener) {
        init();
        mLoadMoreListener = loadMoreListener;
    }

    private OnLoadMoreListener mLoadMoreListener;

    public interface OnLoadMoreListener {
        void loadMore();
    }

    public AutoLoadMoreRecyclerView setAutoLayoutManager(LayoutManager layoutManager) {
        super.setLayoutManager(layoutManager);
        return this;
    }

    public AutoLoadMoreRecyclerView addAutoItemDecoration(ItemDecoration decor) {
        super.addItemDecoration(decor);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoItemAnimator(ItemAnimator anim) {
        super.setItemAnimator(anim);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoAdapter(Adapter adapter) {
        super.setAdapter(adapter);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoHasFixedSize(boolean fixed) {
        super.setHasFixedSize(fixed);
        return this;
    }

    public AutoLoadMoreRecyclerView setAutoItemAnimatorDuration(int duration) {
        super.getItemAnimator().setAddDuration(duration);
        super.getItemAnimator().setMoveDuration(duration);
        super.getItemAnimator().setChangeDuration(duration);
        super.getItemAnimator().setRemoveDuration(duration);
        return this;
    }


}
